interactiveBinomDialog <- function(){
	if( !require(tcltk) ) stop('This function depends on the tcltk package')
	if(!exists('slider.env')) slider.env<<-new.env()
	n <- 10; assign('n',tclVar(n),env=slider.env)
	p <- 0.5; assign('p',tclVar(p),env=slider.env)
	xmin <- 0; assign('xmin',tclVar(xmin),env=slider.env)
	xmax <- 30; assign('xmax',tclVar(xmax),env=slider.env)
	ymin <- 0; assign('ymin',tclVar(ymin),env=slider.env)
	ymax <- 0.5; assign('ymax',tclVar(ymax),env=slider.env)
	sn <- 0  ; assign('sn',tclVar(sn), env=slider.env) #Show normal variable
	binom.refresh <- function(...){
		n <- as.numeric(evalq(tclvalue(n), env=slider.env))
		p <- as.numeric(evalq(tclvalue(p), env=slider.env))
		xmin <- as.numeric(evalq(tclvalue(xmin), env=slider.env))
		xmax <- as.numeric(evalq(tclvalue(xmax), env=slider.env))
		ymin <- as.numeric(evalq(tclvalue(ymin), env=slider.env))
		ymax <- as.numeric(evalq(tclvalue(ymax), env=slider.env))
		xx <- seq(0,n, length=n+1)
		yy <- dbinom(xx,n,p)
		plot(xx,yy,type='h', xlim=c(xmin,xmax), ylim=c(ymin,ymax), ylab='Probability',xlab='x')
		points(xx, dbinom(xx, n, p), pch=16)
		abline(h=0,col="gray")
		# Show standard normal plot
		sn <- as.numeric(evalq(tclvalue(sn), env=slider.env))
		xx <- seq(0,n,length=500)
		if(sn) lines(xx, dnorm(xx,n*p,sqrt(n*p*(1-p))), col='green')
	}
	
	top <- tktoplevel()
	tkwm.title(top,gettext("Interactive Binomial Distribution", domain="R-RcmdrPlugin.TeachingExtras"))
	tkwm.geometry(top,'+0+0')
	
	# n
	tkpack(fr <- tkframe(top),side='top')
	tkpack(tklabel(fr, text=gettext("Binomial trials", domain="R-RcmdrPlugin.TeachingExtras"), width='20'),side='right')
	tkpack(sc <- tkscale(fr, command=binom.refresh, from=1, to=30, orient='horiz', resolution=1, showvalue=T),	side='left')
	assign('sc',sc,env=slider.env)
	evalq(tkconfigure(sc, variable=n),env=slider.env)
	
	# p
	tkpack(fr <- tkframe(top),side='top')
	tkpack(tklabel(fr, text=gettext("Probability of success", domain="R-RcmdrPlugin.TeachingExtras"), width='20'),side='right')
	tkpack(sc <- tkscale(fr, command=binom.refresh, from=0, to=1, orient='horiz', resolution=0.1, showvalue=T),	side='left')
	assign('sc',sc,env=slider.env)
	evalq(tkconfigure(sc, variable=p),env=slider.env)
	
	# show normal
	tkpack(fr <- tkframe(top), side='top')
	tkpack(snlabel<-tklabel(fr, text=gettext("Show Normal", domain="R-RcmdrPlugin.TeachingExtras")), side='left')
	tkpack(sc <- tkcheckbutton(fr, command=binom.refresh), side='left')
	assign('sc', sc, env=slider.env)
	evalq(tkconfigure(sc, variable=sn), env=slider.env)
	
	# xmin
	tkpack(fr <- tkframe(top),side='top')
	tkpack(tklabel(fr, text="Xmin", width=6), side='left')
	tkpack(e <- tkentry(fr,width=8), side='left')
	assign('e',e,env=slider.env)
	evalq(tkconfigure(e, textvariable=xmin), env=slider.env)
	
	# xmax
	tkpack(tklabel(fr, text="Xmax", width=6), side='left')
	tkpack(e <- tkentry(fr,width=8), side='left')
	assign('e',e,env=slider.env)
	evalq(tkconfigure(e, textvariable=xmax), env=slider.env)
	
	# ymin
	tkpack(fr <- tkframe(top),side='top')
	tkpack(tklabel(fr, text="Ymin", width=6), side='left')
	tkpack(e <- tkentry(fr,width=8), side='left')
	assign('e',e,env=slider.env)
	evalq(tkconfigure(e, textvariable=ymin), env=slider.env)
	
	# ymax
	tkpack(tklabel(fr, text="Ymax", width=6), side='left')
	tkpack(e <- tkentry(fr,width=8), side='left')
	assign('e',e,env=slider.env)
	evalq(tkconfigure(e, textvariable=ymax), env=slider.env)
	
	tkpack(tkbutton(top, text=gettext("Refresh", domain="R-RcmdrPlugin.TeachingExtras"), command=binom.refresh), side='left')
	tkpack(tkbutton(top, text=gettext("Exit", domain="R-RcmdrPlugin.TeachingExtras"), command=function()tkdestroy(top)), side='right')
	
}

